package cn.tedu.hotel.service.impl;

import cn.tedu.hotel.ex.ServiceException;
import cn.tedu.hotel.mapper.CustomerMapper;
import cn.tedu.hotel.pojo.dto.CustomerAddNewDTO;
import cn.tedu.hotel.pojo.entity.Admin;
import cn.tedu.hotel.pojo.entity.Customer;
import cn.tedu.hotel.pojo.vo.AdminStandardVO;
import cn.tedu.hotel.pojo.vo.CustomerListItemVO;
import cn.tedu.hotel.pojo.vo.CustomerStandardVO;
import cn.tedu.hotel.service.ICustomerService;
import cn.tedu.hotel.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 *  处理客户相关数据的实现类
 */
@Slf4j
@Service
public class CustomerServiceImpl implements ICustomerService, ServiceCode {

    @Autowired
    CustomerMapper customerMapper;

    public CustomerServiceImpl(){
        log.debug("创建业务对象：CustomerServiceServiceImpl");
    }

    @Override
    public void addNew(CustomerAddNewDTO customerAddNewDTO) {
        log.debug("开始处理【添加客户】的业务，参数：{}",customerAddNewDTO);
        //获取客户证件号 和 手机号
        String idNumber = customerAddNewDTO.getIdNumber();
        String phone = customerAddNewDTO.getPhone();
        // 根据证件号或者手机号查询 客户是否已经存在
        CustomerStandardVO queryResult = customerMapper.getStandardByIdNumberOrPhone(idNumber, phone);
        if (queryResult != null){
            String message = "添加客户失败，手机号或证件号重复";
            log.debug(message);
            throw new ServiceException(ERR_CONFLICT,message);
        }

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //创建客户对象
        Customer customer = new Customer();
        //补全对象中属性的值
        BeanUtils.copyProperties(customerAddNewDTO,customer);
        customer.setGmtCreate(now);
        customer.setGmtModified(now);

        //执行添加
        int rows = customerMapper.insert(customer);
        if (rows != 1){
            String message = "添加客户失败，服务器繁忙，请稍后再试";
            log.debug(message);
            throw new ServiceException(ERR_INSERT,message);
        }




    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始执行【删除客户】的业务，id={}",id);
        //检查表中客户数据是否存在
        CustomerStandardVO queryResult = customerMapper.getStandardById(id);
        if (queryResult == null){
            String message = "删除客户失败，访问数据不存在";
            log.debug(message);
            throw new ServiceException(ERR_NOT_FOUND,message);
        }

        //执行删除
        int rows = customerMapper.deleteById(id);
        if (rows != 1){
            String message = "删除客户失败，服务器繁忙，请稍后再试";
            log.debug(message);
            throw new ServiceException(ERR_DELETE,message);
        }

    }

    @Override
    public void setIsDelete(Long id) {
        log.debug("开始处理【注销客户信息】的业务：id={}", id);
        // 根据id查询客户数据
        CustomerStandardVO queryResult = customerMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：ServiceException：NOT_FOUND
            String message = "标记删除客户数据失败！尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 判断查询结果中的is_delete是否为0
        if (queryResult.getIsDelete() == 0) {
            // 是：ServiceException：CONFLICT
            String message = "标记删除客户失败！当前客户已经标记删除！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // Customer customer = new Customer(); customer.setId(id); customer.setIsDelete(0);
       Customer customer = new Customer();
        customer.setId(id);
        customer.setIsDelete(0);
        // 执行更新，获取返回值
        int rows = customerMapper.update(customer);
        // 判断返回值是否不为1
        if (rows != 1) {
            // 是：ServiceException：UPDATE
            String message = "标记删除客户失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }


    @Override
    public CustomerStandardVO getStandardCustomer(Long id, String userNumber,
                                                  String userName, String idNumber,
                                                  String phone,Integer isDelete) {
        CustomerStandardVO standardCustomer =
                customerMapper.getStandardCustomer(id, userNumber, userName, idNumber, phone, isDelete);
        if (standardCustomer == null){
            String message = "访问数据不存在";
            log.debug(message);
            throw new ServiceException(ERR_NOT_FOUND,message);
        }
        return standardCustomer;
    }


    @Override
    public List<CustomerListItemVO> listCustomers(Long id, String userNumber,
                                                  String userName, String idNumber,
                                                  String phone,Integer isDetele) {
        log.debug("开始处理【查询客户列表】的业务");
        List<CustomerListItemVO> listItemVOS =
                customerMapper.listCustomers(id, userNumber, userName, idNumber, phone,isDetele);
        return listItemVOS;


    }
}
